<?php
/*******************************************************************************

    FinalsClub.org is a platform for students and faculty to collaborate
    and share their knowledge with the world.

    Copyright (C) 2009  Andrew Magliozzi
 
    This file is a part of FinalsClub.org.

    FinalsClub.org is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
    To contact FinalsClub.org, send an email to info@finalsclub.org, or
    send mail to Finals Club, c/o Veritas Tutors, 1132 Massachusetts Avenue,
    Cambridge, MA 02138.

*******************************************************************************/

class CoursePeer extends BaseCoursePeer {

	const REJECTED_STATUS = 0;
	const APPROVED_STATUS = 1;
	const PENDING_STATUS = 2;	
	
	public static function retrieveByNetworkId($network_id = null, $semester_id = null, $status = CoursePeer::APPROVED_STATUS) {
		$c = new Criteria();
		if($network_id) {
			$c->add(CoursePeer::NETWORK_ID, $network_id);
		}
		
		if($semester_id) {
			$c->addJoin(CourseSemesterPeer::COURSE_ID, CoursePeer::ID);			
			$c->add(CourseSemesterPeer::SEMESTER_ID, $semester_id);			
		}
		$c->add(CoursePeer::STATUS, $status);
		$c->addAscendingOrderByColumn(SubjectPeer::NAME);
		$c->addAscendingOrderByColumn(CoursePeer::NAME);
			
		return CoursePeer::doSelectJoinSubject($c);
	}
	
	/**
	 * Retrieves the count of courses for a given subject
	 * @param $subject_id
	 * @param $network_id
	 * @return count
	 */
	public static function countForSubjectId($subject_id, $network_id = null) {
		$c = new Criteria();
		$c->add(CoursePeer::SUBJECT_ID, $subject_id);
		if($network_id) {
			$c->add(CoursePeer::NETWORK_ID, $network_id);
		}
		return NotePeer::doCount($c);
	}

	/**
	 * Retrieve pagination for courses
	 * @param $page
	 * @param $per_page
	 * @param $network_id
	 * @param $semester_id
	 * @param $subject_id
	 * @param $status
	 * @return sfPager
	 */
	public static function retrievePager($page = 1, $per_page = 20, $network_id = null, $semester_id = null, $subject_id = null, $status = CoursePeer::APPROVED_STATUS) {
		$c = new Criteria();
		if($network_id) {
			$c->add(CoursePeer::NETWORK_ID, $network_id);
		}
		
		if($semester_id) {
			$c->addJoin(CourseSemesterPeer::COURSE_ID, CoursePeer::ID);			
			$c->add(CourseSemesterPeer::SEMESTER_ID, $semester_id);		
		}
		
		if($subject_id) {
			$c->add(CoursePeer::SUBJECT_ID, $subject_id);
		}
		
		$c->add(CoursePeer::STATUS, $status);

		$pager = new sfPropelPager('Course', $per_page);
		$pager->setCriteria($c);
		$pager->setPeerMethod('doSelectJoinSubject');
		$pager->setPage($page);
		$pager->init();
		return $pager;
	}

	/**
	 * Retrieve pagination for courses
	 * @param $page
	 * @param $per_page
	 * @param $network_id
	 * @param $semester_id
	 * @param $subject_id
	 * @param $status
	 * @return sfPager
	 */
	public static function retrievePublicPager($page = 1, $per_page = 20, $network_id = null, $semester_id = null, $subject_id = null, $status = CoursePeer::APPROVED_STATUS) {
		$c = new Criteria();
		if($network_id) {
			$c->add(CoursePeer::NETWORK_ID, $network_id);
		}
		
		if($semester_id) {
			$c->addJoin(CourseSemesterPeer::COURSE_ID, CoursePeer::ID);			
			$c->add(CourseSemesterPeer::SEMESTER_ID, $semester_id);		
		}

		$c->addJoin(CoursePeer::ID, NotePeer::COURSE_ID);
		$c->add(NotePeer::TEXT, null, Criteria::ISNOTNULL);	
		$c->add(NotePeer::TEXT, '', Criteria::NOT_EQUAL);	
		$c->add(NotePeer::VISIBILITY, NotePeer::EVERYONE);	

		if($subject_id) {
			$c->add(CoursePeer::SUBJECT_ID, $subject_id);
		}
		$c->addGroupByColumn(CoursePeer::ID);
		
		$c->add(CoursePeer::STATUS, $status);
		$pager = new sfPropelPager('Course', $per_page);
		$pager->setCriteria($c);
		$pager->setPeerMethod('doSelectJoinSubject');
		$pager->setPeerCountMethod('doCountDistinct');
		$pager->setPage($page);
		$pager->init();
		return $pager;
	}


	public static function retrieveById($id) {
		return CoursePeer::retrieveByPK($id);
	}

	/*
	 * Suppose to retrieve all popular courses.
	 * @param $limit
	 * @return array of Course objects
	 */
	public static function retrievePopular($limit = null) {
		$c = new Criteria();
		if($limit) {
			$c->setLimit($limit);
		}
		return CoursePeer::doSelectJoinSubject($c);
	}
	
	public static function retrieveBySubjectId($subject_id) {
		$c = new Criteria();
		$c->add(CoursePeer::SUBJECT_ID, $subject_id);
		return CoursePeer::doSelect($c);
	}
	
	public static function retrieveAll() {
		$c = new Criteria();
		return CoursePeer::doSelect($c);
	}
	
	public static function retrieveBySubjectSPager($page = 1, $per_page = 20, $subject_id = null) {
		$c = new Criteria();		
		if($subject_id) {
			$c->add(CoursePeer::SUBJECT_ID, $subject_id);
		}

		$pager = new sfPropelPager('Course', $per_page);
		$pager->setPeerMethod('doSelect');
		$pager->setCriteria($c);
		$pager->setPage($page);
		$pager->init();
		
		return $pager;
	}
	
	public static function retrieveProfileDataByUserId($user_id) {
		
		//get all courses associated with the users groups that the user did not create
		/*$c = new Criteria();
		$c->addJoin(GroupCoursePeer::COURSE_ID , CoursePeer::ID);
		$c->addJoin(GroupMemberPeer::GROUP_ID, GroupCoursePeer::GROUP_ID);
		$c->add(GroupMemberPeer::USER_ID, $user_id);
		$c->add(CoursePeer::USER_ID, $user_id, Criteria::NOT_EQUAL);
		$group_courses = CoursePeer::doSelect($c);
		
		//get all courses the user created
		$c = new Criteria();
		$c->add(CoursePeer::USER_ID, $user_id);
		$user_courses = CoursePeer::doSelect($c);
		*/
		
		//get all courses that the user follows, decided to do this another way so that I wouldnt have to deal with deleting the courses not followed
		/*$c = new Criteria();
		$c->addJoin(CoursePeer::ID, FollowCoursePeer::COURSE_ID, Criteria::EQUAL);
		$c->add(FollowCoursePeer::COURSE_ID, $user_id);
		$follow_courses = CoursePeer::doSelect($c);
		
		$follow_courses = FollowCoursePeer::retrieveByUserId($user_id);
		*/
		
		
		//	return array_merge($group_courses, $user_courses, $follow_courses);
	
		$c = new Criteria();
		$c->addJoin(CoursePeer::ID, FollowCoursePeer::COURSE_ID, Criteria::EQUAL);
		$c->add(FollowCoursePeer::USER_ID, $user_id);
		$follow_courses = CoursePeer::doSelect($c);
		
		return $follow_courses;
	}

	/**
	 * Method to DELETE all rows from the course table.
	 * Overrides the BaseCoursePeer doDeleteAll
	 *
	 * @return     int The number of affected rows (if supported by underlying database driver).
	 * @see	   BaseCoursePeer::doDeleteAll()
	 */
	public static function doDeleteAll($con = null)
	{
		if (file_exists($index = Search::getLuceneIndexFile(Search::COURSE))) {
    			sfToolkit::clearDirectory($index);
    			rmdir($index);
  		}
		return parent::doDeleteAll($con);
	}
	
	//retrieve the courses that the user has created
	public static function retrieveByUserId($user_id)
	{
		$c = new Criteria();
		$c->add(CoursePeer::USER_ID, $user_id);
		return CoursePeer::doSelect($c);
	}

	public static function doCountDistinct(Criteria $criteria, $distinct = false, PDO $con = null){
		return self::doCount($criteria, true ,$con);
	}
	
	
}
